<template>
  <!--操作栏：编辑对话框-->
  <el-dialog :model-value="visible" @close="dialogClose" width="30%">
    <!--标题-->
    <template #header>
      <div style="font-size: 18px; color: #409eff; font-weight: bold">修改工单分配信息</div>
    </template>
    <el-form :model="row" ref="formRef" :rules="formRules" label-width="120px">
      <!-- 配置处理人选择，通过下拉框选择-->
      <el-form-item label="处理人：" prop="processed_people">
        <el-select class="m-2" v-model="row.processed_people" @click="getUser" placeholder="请选择" style="width: 200%;">
          <el-option v-for="row in username" :key="row.name" :label="`${row.name}`" :value="row.name"></el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="dialogClose">取消</el-button>
        <el-button type="primary" @click="dialogWorkOrderAllocation_btn">确定</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script>
export default {
  name: 'WorkOrderAllocation',
  // 介绍父组件的值
  props: {
    visible: Boolean, // 获取dialog是否打开变量
    row: {
      type: [Object, String, Number],
      default: 0
    }
  },
  data() {
    return {
      username: '', //获取处理人
      formRules: {
        processed_people: [{ required: true, message: '请选择分配用户', trigger: 'change' }]
      }
    }
  },
  methods: {
    // 点击关闭，子组件通知父组件更新属性
    dialogClose() {
      this.$emit('update:visible', false) // 父组件必须使用 v-model
    },

    // 获取idc机房信息
    getUser() {
      this.$http.get('api/user/?page_size=100').then(res => {
        this.username = res.data.data
      })
    },
    dialogWorkOrderAllocation_btn() {
      // 验证表单是否通过
      this.$refs.formRef.validate(valid => {
        if (valid) {
          // 重新赋值
          this.row.status = '处理中'
          this.$http
            .put('api/my_work_order/' + this.row.id + '/', this.row)
            .then(res => {
              if (res.data.code == 200) {
                // 反馈请求接口情况
                this.$message.success('分配工单成功')
                
                // 关闭弹出窗口
                this.dialogClose()
                this.$parent.GetAllMyWorkOrder() // 调用父组件方法，更新数据
              } else {
                this.$message.info('分配工单失败')
              }
            })
            .catch(error => {
              this.$message.error('服务服务端接口请求错误！' + error)
            })
        } else {
          console.log('格式错误')
        }
      })
    }
  }
}
</script>

<style scoped></style>
